import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-09-12
 * Time: 15:57
 */
public class SeqList {
    private int[] elem;
    private int usedSize;  //记录当前有多少个有效数字
    public SeqList(){
        this.elem = new int[2];
    }
    // 打印顺序表，注意：该方法并不是顺序表中的方法，为了方便看测试结果给出的
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i] + " ");
        }
    }
        // 新增元素,默认在数组最后新增
    public void add(int data) {
        if (this.usedSize == this.elem.length) {
            this.elem = Arrays.copyOf(this.elem, 2 * usedSize);
        }
        this.elem[this.usedSize] = data;
        usedSize++;
    }
        // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]){
                return true;
            }
        }
        return false;
    }
        // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]){
                return i;
            }
        }
        return -1;
    }
        // 获取 pos 位置的元素
    public int get(int pos) {
        if (pos < 0 || pos >=this.usedSize){
            throw new PosOutBoundsException("获取数据时，位置不合法");
        }
        return this.elem[pos];
    }
        // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
        // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        if (pos < 0 || pos >= this.usedSize){
            throw new PosOutBoundsException("获取数据时，位置不合法");
        }
        this.elem[pos] = value;
    }
    //添加pos位置数据位data
    public void add(int pos, int data) {
        if (pos < 0 || pos > this.usedSize){
            throw new PosOutBoundsException("获取数据时，位置不合法");
        }
        if (this.usedSize == this.elem.length) {
            this.elem = Arrays.copyOf(elem, 2 * this.usedSize);
        }
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        int pos = indexOf(toRemove);

        if (pos == -1){
            throw new ValueOutBoundsException("没有你查找的数据");
        }
        for (int i = pos; i < this.usedSize - 1; i++){
            this.elem[i] = this.elem[i + 1];
        }
        this.usedSize--;
//        this.elem[this.usedSize] = null;
    }

    // 清空顺序表
    public void clear() {
//        for (int i = 0; i < this.usedSize; i++) {
//            this.elem[i] = null;
//        }
        this.usedSize = 0;
    }
}








